﻿proc import out=work.test16 datafile="I:/prepare/pseudo_data.dta" dbms=dta replace;run; 
proc import out=work.crypto_factor datafile="I:/prepare/crypto_factor.dta" dbms=dta replace;run;  

data test17; set test16;
holding_date = intnx("day",date, 1,"e");
format holding_Date MMDDYY10.;
run;

proc sql;
	create table test18 as select a.*, (a.holding_ret_daily-b.rf_daily) as holding_excess_ret_daily
		from test17 as a left join crypto_factor as b on 
		a.holding_date=b.date_temp;
	run;

data test19; set test18; if  year=2019 or (year=2018 and month>=8) or year=2020 or year=2021;
/*Table 4*/

/*Table 5: Use the following.*/
/*if s_constraint=1;*/ /*constrained crypto assets*/

/* Table A.4: Use one of the following.
if dapps = 1;
if dapps = 0;
if own = 1;
if own = 0;
if noborder= 1;
if noborder = 0;
*/
run;

proc sort data=test19;by date;run;

proc reg data=test19 outest=FB noprint;
	by date;
	model holding_excess_ret_daily=  ab_turn_daily_new_30 cv_turn reversal  abs_reversal mcap mom_1_11  illiq IVOL ab_svi_median  max_daily/adjrsq;
quit;

** 2. drop irrelevant estimates;

proc sort data=FB;	by _model_ date;
run;

data FB2;
	set FB;
	drop _TYPE_ _DEPVAR_ _RMSE_ _IN_ _P_ _EDF_;
	rename _model_=model;
run;

proc transpose data=FB2 out=FBny name=name prefix=coef;
	by model date;
run;

data FBny;
	set FBny;
	retain code;
	by model date;
	code=code+1;

	if first.date then
		code=1;
run;

proc sort data=FBny;by model code name;run;

** 3. Newey-West t-stat for the time-series average of coefficients;
%let lag=8;
*lags for Newey-West t-stat;

proc model data=FBny;
	by model code name;
	parms a;
	exogenous coef1;
	instruments / intonly;
	coef1=a;
	fit coef1 / gmm kernel=(bart, %eval(&lag+1), 0);
	ods output parameterestimates=param1 fitstatistics=fitresult 
		OutputStatistics=residual;
quit;

/********************/
data param1;
	set param1;
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
	T=compress('('||tvalue2||')');
	PARAM=compress(put(estimate, 7.3)||p);
run;

data param1a;
	set param1;
	keep model code name coef _name_;
	_name_='PARAM';
	coef=PARAM;
run;

data param1b;
	set param1;
	keep model code name coef _name_;
	_name_='T';
	coef=T;
run;

data param2;set param1a param1b;run;

proc sort data=param2;by code _name_ model;run;

proc transpose data=param2 out=param3;by code name _name_;id model;var coef;run;

data param3;
	set param3;

	if _name_='T' then
		do;
			code=.;
			name=.;
		end;
run;

** 5. find the range of periods and obs used;

proc sort data=fb out=fb3;by _model_;run;

data fb3;
	set fb3;
	keep _model_ date num;
	num=_edf_+_p_;
	rename _model_=model;
run;

proc sql;
	create table num(where=(model='MODEL1')) as select model, min(date) as start, 
		max(date) as end, count(date) as range, sum(num) as obs from fb3 group by 
		model;
quit;

proc transpose data=num out=num;
	by model;
	var start -- obs;
run;

data num;
	set num;
	rename _name_=name;
	MODEL1=put(col1, 7.0);
	drop model col1;
run;

data param3;set param3 num;run;

